﻿#region Using directives
using System;
using System.ComponentModel;
using System.Web.Script.Services;
using System.Web.Services;

#endregion

namespace ProcolServices {
    /// <summary>
    /// Summary description for ProjectCommentService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [ToolboxItem(false)]
    [ScriptService]
    public class ProjectCommentService : WebService {
        private readonly ProcolDataContext db = new ProcolDataContext();

        [WebMethod]
        public int AddNewProjectComment(int commentId, int userId, int projectId, String comment) {
            var values = new Object[] {commentId, userId, projectId, comment};
            foreach (var value in values) {
                String str = value.ToString();
                int tryParse;

                if (Int32.TryParse(str, out tryParse)) {
                    if (tryParse < 0)
                        return -2;
                } else if (String.IsNullOrEmpty(str))
                    return -2;
            }
            try {
                var newProjectCom = new ProjectComment
                {CommentId = commentId, UserId = userId, ProjectId = projectId, Comment = comment};
                db.ProjectComments.InsertOnSubmit(newProjectCom);
                db.SubmitChanges();

                return newProjectCom.CommentId;
            } catch (InvalidOperationException) {
                return -2;
            }
        }
    }
}